//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#pragma package(smart_init)
#pragma link "OpenDirectory"
#pragma link "CShowProgress"
#pragma resource "*.dfm"

//---------------------------------------------------------------------------
#include "vSImporter.h"
#pragma link "CComboBx"
#pragma link "CCheckB"
//---------------------------------------------------------------------------
//                          CONSTRUCTEUR
//---------------------------------------------------------------------------
__fastcall TfImport::TfImport(const int &nLogiciel, const int &nVersion, TComponent* Owner)
    : TForm(Owner)
{
    TdmWebHelp * dmWebHelp = NULL;
    try {
        dmWebHelp = new TdmWebHelp((TComponent*)0);
        //Icones
        //------
        bbQuitter->Glyph->LoadFromResourceName( (int)HInstance, "ID_ACTION_QUITTER");
        bbImprimer->Glyph->LoadFromResourceName( (int)HInstance, "ID_ACTION_IMPRIMER");
        bbApercu->Glyph->LoadFromResourceName( (int)HInstance, "ID_ACTION_VISUALISER");
        bbSelect->Glyph->LoadFromResourceName((int)HInstance, "ID_ACTION_SELECT_ALL");
        bbUnSelect->Glyph->LoadFromResourceName((int)HInstance, "ID_ACTION_DESELECT");

        nColumnToSort = 0;
        nLastColumn   = 0;
        bAscToDesc    = true;


        //Composant TStringList Ecran, Object et référence
        //------------------------------------------------
        m_lRef   = new TStringList();
        m_lObj   = new TStringList();
        m_lEcran = new TStringList();
        try {
            dmWebHelp->tLogiciels->Active = true;
            dmWebHelp->tLogiciels->First();
            //Ajout de tous les Logiciels
            //---------------------------
            while(!dmWebHelp->tLogiciels->Eof) {
                cbLogiciel->Items->AddObject(astLogiciels(LOG_NOM), (TObject*) ntLogiciels(LOG_CODE) );
                dmWebHelp->tLogiciels->Next();
            }

            dmWebHelp->tVersions->Active = true;
            dmWebHelp->tVersions->First();
            //Ajout de toutes les Versions
            //----------------------------
            while(!dmWebHelp->tVersions->Eof) {
                if(ntVersions(LOG_CODE) == nLogiciel)
                    cbVersion->Items->AddObject(astVersions(VER_NOM), (TObject*)ntVersions(VER_CODE) );
                dmWebHelp->tVersions->Next();
            }

            LogVerSelectionner(nLogiciel, nVersion);
            Afficher();
        }catch (Exception &e ) {
            ShowMessage("Problème de connection à la base WebHelp");}
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(dmWebHelp) {delete dmWebHelp; dmWebHelp = NULL;}
}
//---------------------------------------------------------------------------
//                          DESTRUCTEUR
//---------------------------------------------------------------------------
__fastcall TfImport::~TfImport()
{
    try {
        //Liste des référence à importer
        if(m_lEcran) {delete m_lEcran; m_lEcran = NULL;}
        if(m_lObj) {delete m_lObj; m_lObj = NULL;}
        if(m_lRef) {delete m_lRef; m_lRef = NULL;}
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
//     AFFICHAGE DU LOGICIEL ET DE LA VERSION SELECTIONNEE PRECEDEMENT
//---------------------------------------------------------------------------
void __fastcall TfImport::LogVerSelectionner(const int &nLogiciel, const int &nVersion)
{
    try {
        for(int i=0;i<cbLogiciel->Items->Count;i++) {
            if(nLogiciel == (int)cbLogiciel->Items->Objects[i])
                cbLogiciel->ItemIndex = i;
        }

        for(int j=0;j<cbVersion->Items->Count;j++) {
            if(nVersion == (int)cbVersion->Items->Objects[j])
                cbVersion->ItemIndex = j;
        }
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
//                        CHANGER DE LOGICIEL
//---------------------------------------------------------------------------
void __fastcall TfImport::cbLogicielChange(TObject *Sender)
{
    TdmWebHelp * dmWebHelp = NULL;
    try {
        cbVersion->Clear();
        dmWebHelp = new TdmWebHelp((TComponent*)0);
        dmWebHelp->tVersions->Active = true;
        dmWebHelp->tVersions->First();
        while(!dmWebHelp->tVersions->Eof) {
            if(ntVersions(LOG_CODE) == (int)cbLogiciel->Items->Objects[cbLogiciel->ItemIndex])
                cbVersion->Items->AddObject(astVersions(VER_NOM), (TObject*)ntVersions(VER_CODE) );
            dmWebHelp->tVersions->Next();
        }
        cbVersion->ItemIndex = 0;

        if(cbVersion->Items->Count != 0) {
            Afficher();
        }else {
            lvListe->Items->Clear();
            MessageDlg("Aucune version aide attachée au logiciel.", mtInformation, TMsgDlgButtons() << mbOK, 0);
        }
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(dmWebHelp) {delete dmWebHelp; dmWebHelp = NULL;}
}
//---------------------------------------------------------------------------
//                        CHANGER DE VERSION
//---------------------------------------------------------------------------
void __fastcall TfImport::cbVersionChange(TObject *Sender)
{
    try {
        Afficher();
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
//                          MISE A JOUR DES BASES
//---------------------------------------------------------------------------
void __fastcall TfImport::bbLireClick(TObject *Sender)
{
    TdmWebHelp * dmWebHelp = NULL;
    TfPatienter * fPatienter = NULL;
    CHistorique * Hist = NULL;
    try {
        bool bValidationSiDoublon = true;
        //Vider les listes en mémoires
        //----------------------------
        m_lEcran->Clear();
        m_lObj->Clear();
        m_lRef->Clear();

        if(codSelect->Execute()) {
            //Vider la table tampon lien contenant les références webhelp dans les programmes
            //-------------------------------------------------------------------------------
            ViderTableLien();

            fPatienter = new TfPatienter((TComponent*)0);
            fPatienter->PatienterOuvre();
            RechercherDfm( codSelect->Repertoire );
            fPatienter->PatienterFerme();


            //Inscription du résultat dans la table Lien (Table tampon)
            dmWebHelp = new TdmWebHelp((TComponent*)0);
            dmWebHelp->tLien->Active = true;
            for( int i=0; i< m_lRef->Count ; i++) {
                dmWebHelp->tLien->Insert();
                ntLien(LOG_CODE)        = (int)cbLogiciel->Items->Objects[cbLogiciel->ItemIndex];
                ntLien(VER_CODE)        = (int)cbVersion->Items->Objects[cbVersion->ItemIndex];
                astLien(LIEN_REF)       = m_lRef->Strings[i];
                astLien(LIEN_ECRAN)     = m_lEcran->Strings[i];
                astLien(LIEN_OBJECT)    = m_lObj->Strings[i];
                dmWebHelp->tLien->Post();
                dmWebHelp->trWebHelp->Commit();
            }

            if(VerifierDoublon()){
                TListerDoublon * fDoublon = NULL;
                fDoublon = new TListerDoublon(  (int)cbLogiciel->Items->Objects[cbLogiciel->ItemIndex],
                                                (int)cbVersion->Items->Objects[cbVersion->ItemIndex],
                                                Application);
                if( fDoublon->ShowModal() == mrAbort) {
                    bValidationSiDoublon = false;
                    lvListe->Items->Clear();
                    ViderTableLien();
                }
                if(fDoublon) {delete fDoublon; fDoublon = NULL;}
            }

            if(bValidationSiDoublon)
                Afficher();

            Hist = new CHistorique();
            Hist->AjouterOperation(Histo_MAJ_BASE,
                                    cbLogiciel->Items->Strings[cbLogiciel->ItemIndex],
                                    cbVersion->Items->Strings[cbVersion->ItemIndex]);
        }
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
    }
    if(Hist) {delete Hist; Hist = NULL;}
    if(fPatienter) {delete fPatienter; fPatienter = NULL;}
    if(dmWebHelp) {delete dmWebHelp; dmWebHelp = NULL;}
}
//---------------------------------------------------------------------------
//                      RECHERCHER FICHIER *.dfm
//---------------------------------------------------------------------------
void __fastcall TfImport::RechercherDfm( const AnsiString &asRepertoire )
{
    try {
        TSearchRec sr;
        if(!FindFirst(IncludeTrailingBackslash(asRepertoire) + "*.*",faAnyFile,sr)) {
            do{
                if( sr.Attr &= faDirectory ) {
                    if(sr.Name != "." && sr.Name != "..")
                        RechercherDfm(IncludeTrailingBackslash(asRepertoire) + sr.Name);
                }else {
                    if(ExtractFileExt(sr.Name) == ".dfm")
                        TraiterDFM(IncludeTrailingBackslash(asRepertoire)+sr.Name, sr.Name);
                }
            }while(!FindNext(sr));
        }
        FindClose(sr);
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
//              LECTURE DES FICHIERS *.dfm
//---------------------------------------------------------------------------
void __fastcall TfImport::TraiterDFM( const AnsiString &asFichier, const AnsiString &asNomFichier )
{
    char * cLigne = NULL;
    try {
        char cObjet[1024];
        char cRef[1024];
        bool bRefTrouver = false;
        AnsiString Object = "";
        cLigne = new char[500 + 1];
        ifstream Fic(asFichier.c_str(), ios::in);
        if(Fic.is_open() ) {
            do{
                Fic.getline(cLigne, 500, '\n');
                //Récupération de la référence sur les composants EIC
                if( strstr(cLigne, "object") != 0 )
                    GetObject(cLigne, cObjet);
                else if( strstr(cLigne, "RefWebHelp = '") != 0)
                    bRefTrouver = true;

                //Récupération de la référence sur la CForm
                if( strstr(cObjet, "lRefWebHelp") != 0 ){
                    if( strstr(cLigne, "Caption = '") != 0) {
                        bRefTrouver = true;
                    }
                }

                if(bRefTrouver) {
                    GetRef(cLigne, cRef);

                    m_lEcran->Add( asNomFichier );
                    m_lObj->Add( cObjet );
                    m_lRef->Add( cRef );
                    bRefTrouver = false;
                }
            }while(!Fic.eof());
        }
        Fic.close();
    }catch(Exception &e) {
        EIC_DISPLAY_EXCEPTION(e);
        cLigne = NULL;}
    if(cLigne)  {delete [] cLigne; cLigne = NULL;}
}
//---------------------------------------------------------------------------
//                      Obtenir l'objet
//---------------------------------------------------------------------------
void __fastcall TfImport::GetObject(char * cLigne, char * cObj)
{
    try {
        strcpy(cObj ,strtok(cLigne, " :"));
        strcpy(cObj ,strtok(NULL, " :"));
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
//                      Obtenir la référence
//---------------------------------------------------------------------------
void __fastcall TfImport::GetRef(char * cLigne, char * cRefer)
{
    try {
        strcpy(cRefer, strtok(cLigne, "'"));
        strcpy(cRefer, strtok(NULL, "'"));
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
//          Vérification des doublons dans les liens programme
//---------------------------------------------------------------------------
bool __fastcall TfImport::VerifierDoublon()
{
    bool bRetour = false;
    try {
        int i, j;
        for(i=0; i<m_lRef->Count-1; i++) {
            for(j=i+1; j < m_lRef->Count; j++) {
                if( (m_lRef->Strings[i] == m_lRef->Strings[j]) )
                    bRetour = true;
            }
        }
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
        bRetour = false;}
    return bRetour;
}
//---------------------------------------------------------------------------
//                          AFFICHER
//---------------------------------------------------------------------------
void __fastcall TfImport::Afficher()
{
    TdmWebHelp * dmWebHelp = NULL;
    TZZMySqlQuery * MaRequete = NULL;
    try {
        lvListe->Items->Clear();
        AnsiString asRequeteAAfficher = ChoisirRequete();
        if(!asRequeteAAfficher.IsEmpty()) {
            dmWebHelp = new TdmWebHelp((TComponent*)0);

            TListItem  *ListItem;
            MaRequete = new TZZMySqlQuery( (TComponent*)0 );
            MaRequete->Database = dmWebHelp->dbWebHelp;

            MaRequete->Sql->Text = asRequeteAAfficher;

            MaRequete->Open();
            MaRequete->First();

            while(!MaRequete->Eof) {
                ListItem = lvListe->Items->Add();
                //Reference WebHelp
                //-----------------
                if(!asRequete(0).IsEmpty())
                    ListItem->SubItems->Add(asRequete(0));
                else
                    ListItem->SubItems->Add("<Néant>");

                //Lien Programme
                //--------------
                if(!asRequete(1).IsEmpty())
                    ListItem->SubItems->Add(asRequete(1));
                else
                    ListItem->SubItems->Add("<Néant>");

                MaRequete->Next();
            }

            MaRequete->Sql->Clear();
            MaRequete->Close();
        }
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
    if(dmWebHelp) {delete dmWebHelp; dmWebHelp = NULL;}
}
//---------------------------------------------------------------------------
//                   VIDER LA TABLE TAMPON LIEN
//---------------------------------------------------------------------------
void __fastcall TfImport::ViderTableLien( void )
{
    TdmWebHelp * dmWebHelp = NULL;
    TZZMySqlQuery * MaRequete = NULL;
    try {
        int nIdxLog = cbLogiciel->ItemIndex;
        int nIdxVer = cbVersion->ItemIndex;

        if( (nIdxLog != AUCUNE_SELECTION) && (nIdxVer != AUCUNE_SELECTION) ) {
            int nLogiciel = (int)cbLogiciel->Items->Objects[nIdxLog];
            int nVersion  = (int)cbVersion->Items->Objects[nIdxVer];
            dmWebHelp = new TdmWebHelp((TComponent*)0);
            MaRequete = new TZZMySqlQuery( (TComponent*)0);
            MaRequete->Database = dmWebHelp->dbWebHelp;

            MaRequete->Sql->Text = (AnsiString) "delete from lien "+
                                                "where log_code = "+nLogiciel+
                                                " and ver_code = "+nVersion;

            MaRequete->ExecSql();
            MaRequete->Sql->Clear();

            MaRequete->Sql->Text = "Commit";
            MaRequete->ExecSql();
            MaRequete->Sql->Clear();

            MaRequete->Close();
        }
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
    if(dmWebHelp) {delete dmWebHelp; dmWebHelp = NULL;}
}
//---------------------------------------------------------------------------
//                   CREER DES REFERENCES AUTO
//---------------------------------------------------------------------------
void __fastcall TfImport::bCreerClick(TObject *Sender)
{
    TdmWebHelp * dmWebHelp = NULL;
    TStringList * lReference = NULL;
    CHistorique * Hist = NULL;
    try {
        int nIdxLog = cbLogiciel->ItemIndex;
        int nIdxVer = cbVersion->ItemIndex;

        if( (nIdxLog != AUCUNE_SELECTION) && (nIdxVer != AUCUNE_SELECTION) ) {
            int nLogiciel = (int)cbLogiciel->Items->Objects[nIdxLog];
            int nVersion = (int)cbVersion->Items->Objects[nIdxVer];
            bool bFlag = false;

            lReference = new TStringList();
            AnsiString asMaRef = "";
            TItemStates selected = TItemStates() << isSelected;
            TListItem  *ListItem = lvListe->Selected;
            if(lvListe->SelCount != 0) {
                while(ListItem) {
                    asMaRef = ListItem->SubItems->Strings[1];
                    if(asMaRef != ListItem->SubItems->Strings[0] && asMaRef != NEANT) 
                        lReference->Add(asMaRef);

                    if(asMaRef == NEANT)
                        bFlag = true;
                    ListItem = lvListe->GetNextItem(ListItem,sdAll,selected);
                }

                if(lReference->Count == 0){
                    if(!bFlag) {
                        if(lvListe->SelCount > 1)
                            MessageDlg("Il n'y a pas d'écart entre les références Webhelp et les références programme.", mtInformation, TMsgDlgButtons() << mbOK, 0);
                        else
                            MessageDlg("Il n'y a pas d'écart entre cette référence Webhelp et cette référence programme. ", mtInformation, TMsgDlgButtons() << mbOK, 0);
                    }else {
                        MessageDlg("Il y a des écarts entre les références WebHelp et les références \nprogrammes", mtInformation, TMsgDlgButtons() << mbOK, 0);
                    }
                }else {
                    dmWebHelp = new TdmWebHelp((TComponent*)0);
                    dmWebHelp->tRefer->Active = true;
                    for(int i=0; i<lReference->Count; i++) {
                        dmWebHelp->tRefer->Insert();
                        ntRefer(LOG_CODE)       = nLogiciel;
                        ntRefer(VER_CODE)       = nVersion;
                        astRefer(REF_NOM)       = lReference->Strings[i];
                        ntRefer(REF_ATTENTE)    = IDX_EN_COURS_MISE_A_JOUR;
                        dmWebHelp->tRefer->Post();
                        dmWebHelp->trWebHelp->Commit();
                    }
                    Afficher();
                }


                Hist = new CHistorique();
                for(int j=0; j<lReference->Count; j++) {
                    Hist->AjouterOperation(Histo_REF_AUTO,
                                            cbLogiciel->Items->Strings[nIdxLog],
                                            cbVersion->Items->Strings[nIdxVer],
                                            lReference->Strings[j]);
                }

            }else
                EICAttention(ERREUR_SELECTION);
        }
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(lReference) {delete lReference; lReference = NULL;}
    if(Hist)       {delete Hist; Hist = NULL;}
    if(dmWebHelp)  {delete dmWebHelp; dmWebHelp = NULL;}
}
//---------------------------------------------------------------------------
//              IMPRESSION
//---------------------------------------------------------------------------
void __fastcall TfImport::bbImprimerClick(TObject *Sender)
{
    try {
        bool bFlag = false;
        for(int i=0; i<lvListe->Items->Count; i++) {
            if(lvListe->Items->Item[i]->SubItems->Strings[0] == NEANT)
                bFlag = true;
            if(lvListe->Items->Item[i]->SubItems->Strings[1] == NEANT)
                bFlag = true;
        }
        if(bFlag)
            Edition(false);
        else
            MessageDlg("Il n'y a aucun écart entre les références WebHelp et les \nréférences programme.", mtInformation, TMsgDlgButtons() << mbOK, 0);
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
//              VISUALISATION
//---------------------------------------------------------------------------
void __fastcall TfImport::bbApercuClick(TObject *Sender)
{
    try {
        bool bFlag = false;
        for(int i=0; i<lvListe->Items->Count; i++) {
            if(lvListe->Items->Item[i]->SubItems->Strings[0] == NEANT)
                bFlag = true;
            if(lvListe->Items->Item[i]->SubItems->Strings[1] == NEANT)
                bFlag = true;
        }
        if(bFlag)
            Edition(true);
        else
            MessageDlg("Il n'y a aucun écart entre les références WebHelp et les \nréférences programme.", mtInformation, TMsgDlgButtons() << mbOK, 0);
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
//          GERE L IMPRESSION OU LA VISUALISATION
//---------------------------------------------------------------------------
void __fastcall TfImport::Edition(bool bVisu)
{
    HINSTANCE hApercu   = NULL;
    try {
        int nIdxLog = cbLogiciel->ItemIndex;
        int nIdxVer = cbVersion->ItemIndex;

        if( (nIdxLog != AUCUNE_SELECTION) && (nIdxVer != AUCUNE_SELECTION) ) {

            int nLogiciel = (int)cbLogiciel->Items->Objects[nIdxLog];
            int nVersion = (int)cbVersion->Items->Objects[nIdxVer];

            char cLogiciel[50] = "";
            char cVersion[50]  = "";

            GetLogiciel( cLogiciel );
            GetVersion( cVersion );

            AnsiString asDLL =  IncludeTrailingBackslash(IncludeTrailingBackslash(ExtractFilePath(ParamStr(0)))+"WHEdit")+DLL_WHEDIT;
        
            //Déclaration du pointeur de fonction
            typedef void (__stdcall *TWH_EditRef)( char*,char*,int,int,bool);
            TWH_EditRef pWH_EditRef = NULL;

            //Chargement de la dll
            if ((hApercu = LoadLibrary( asDLL.c_str() )) == NULL) {
                throw EAbort("Impossible de charger : WHEdit.DLL");
            }

            //Récupération du pointeur
            pWH_EditRef = (TWH_EditRef)GetProcAddress(hApercu, "_WH_EditRef");
            if( !pWH_EditRef )
                throw EAbort("WH_EditRef est corrompue");

            //Appelle de la fonction
            pWH_EditRef(cLogiciel,cVersion,nLogiciel,nVersion,bVisu );

            //Relancer la connection à la base WebHelp
            TdmWebHelp * dmWebHelp = NULL;
            dmWebHelp = new TdmWebHelp((TComponent*)0);
            if(dmWebHelp) {delete dmWebHelp; dmWebHelp = NULL;}
        }

    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(hApercu) {FreeLibrary(hApercu); hApercu   = NULL;}
}
//---------------------------------------------------------------------------
//          Obtenir le nom du logiciel
//---------------------------------------------------------------------------
void __fastcall TfImport::GetLogiciel( char * cLog)
{
    TdmWebHelp * dmWebHelp = NULL;
    TZZMySqlQuery * MaRequete = NULL;
    int nLog = 0;
    try {
        int nIdxLog = cbLogiciel->ItemIndex;
        if( nIdxLog != AUCUNE_SELECTION) {
            nLog = (int)cbLogiciel->Items->Objects[nIdxLog];

            try {
                dmWebHelp = new TdmWebHelp((TComponent*)0);
                MaRequete = new TZZMySqlQuery((TComponent*)0);
                MaRequete->Database = dmWebHelp->dbWebHelp;

                MaRequete->Sql->Text =(AnsiString)"select log_nom from logiciels where log_code="+nLog;

                MaRequete->Open();
                MaRequete->First();

                if( !asRequete(0).IsEmpty() )
                    strcpy(cLog, asRequete(0).c_str());

                MaRequete->Sql->Clear();
                MaRequete->Close();
            }catch (Exception &e ) {
                ShowMessage("Problème de connection à la base WebHelp");}
        }
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
    if(dmWebHelp) {delete dmWebHelp; dmWebHelp = NULL;}
}
//---------------------------------------------------------------------------
//          Obtenir le nom de la version
//---------------------------------------------------------------------------
void __fastcall TfImport::GetVersion( char * cVer )
{
    TdmWebHelp * dmWebHelp = NULL;
    TZZMySqlQuery * MaRequete = NULL;
    int nVer = 0;
    try {
        int nIdxVer = cbVersion->ItemIndex;
        if( nIdxVer != AUCUNE_SELECTION) {
            nVer = (int)cbVersion->Items->Objects[nIdxVer];

            try {
                dmWebHelp = new TdmWebHelp((TComponent*)0);
                MaRequete   = new TZZMySqlQuery( (TComponent*)0);
                MaRequete->Database = dmWebHelp->dbWebHelp;

                MaRequete->Sql->Text =(AnsiString)"select ver_nom from versions where ver_code="+nVer;

                MaRequete->Open();
                MaRequete->First();

                if( !asRequete(0).IsEmpty() )
                    strcpy(cVer, asRequete(0).c_str());

                MaRequete->Sql->Clear();
                MaRequete->Close();
            }catch (Exception &e ) {
                ShowMessage("Problème de connection à la base WebHelp");}
        }
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
    if(dmWebHelp) {delete dmWebHelp; dmWebHelp = NULL;}
}
//---------------------------------------------------------------------------
//          SUPPRIMER LES REFERENCES
//---------------------------------------------------------------------------
void __fastcall TfImport::bSupprimerClick(TObject *Sender)
{
    TStringList * RefNom = NULL;
    CHistorique * Hist = NULL;
    try {
        bool bAdresseTrouver = false;
        int nIdxLog = cbLogiciel->ItemIndex;
        int nIdxVer = cbVersion->ItemIndex;

        if( (nIdxLog != AUCUNE_SELECTION) && (nIdxVer != AUCUNE_SELECTION) ) {
            int nLogiciel = (int)cbLogiciel->Items->Objects[nIdxLog];
            int nVersion = (int)cbVersion->Items->Objects[nIdxVer];

            bool bAdresse = false;
            RefNom = new TStringList();
            TItemStates selected = TItemStates() << isSelected;
            TListItem  *ListItem = lvListe->Selected;

            if(lvListe->SelCount != 0 ) {
                while(ListItem) {
                    if(ListItem->SubItems->Strings[0] != NEANT)  //Stocker les références sélectionnées sauf Néant
                        RefNom->Add(ListItem->SubItems->Strings[0]);
                    ListItem = lvListe->GetNextItem(ListItem,sdAll,selected);
                }

                //Boucle de vérification afin de savoir si la référence possede une adresse Web
                for(int i=0; i<RefNom->Count; i++) {
                    if(GetAdresse(RefNom->Strings[i])){
                        bAdresse = true;
                        break;
                    }
                }

                if(RefNom->Count != 0) {
                    TSupprimer * fSupprimer = NULL;
                    fSupprimer = new TSupprimer(nLogiciel,nVersion,RefNom,bAdresse,Application);
                    if(fSupprimer->ShowModal() == mrOk)
                        bAdresseTrouver = true;
                    if(fSupprimer) {delete fSupprimer; fSupprimer = NULL;}
                }

                if(bAdresseTrouver) {
                    Hist = new CHistorique();
                    for(int j=0; j<RefNom->Count; j++) {
                        Hist->AjouterOperation(Histo_SUPPR_REF,
                                                cbLogiciel->Items->Strings[cbLogiciel->ItemIndex],
                                                cbVersion->Items->Strings[cbVersion->ItemIndex],
                                                RefNom->Strings[j]);
                    }

                }
                Afficher();
            }else {
                MessageDlg("Veuillez sélectionner au moins une référence à supprimer", mtWarning, TMsgDlgButtons() << mbOK, 0);
            }
        }
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
        RefNom = NULL;}
    if(Hist) {delete Hist; Hist = NULL;}
    if(RefNom) {delete RefNom; RefNom = NULL;}
}
//---------------------------------------------------------------------------
//          Vérifier si une adresse existe pour une référence donnée
//---------------------------------------------------------------------------
bool __fastcall TfImport::GetAdresse(const AnsiString &asRef)
{
    TdmWebHelp * dmWebHelp = NULL;
    bool bRetour = false;
    TZZMySqlQuery * MaRequete = NULL;
    try {
        int nIdxLog = cbLogiciel->ItemIndex;
        int nIdxVer = cbVersion->ItemIndex;

        if( (nIdxLog != AUCUNE_SELECTION) && (nIdxVer != AUCUNE_SELECTION) ) {
            int nLogiciel = (int)cbLogiciel->Items->Objects[nIdxLog];
            int nVersion  = (int)cbVersion->Items->Objects[nIdxVer];
            dmWebHelp = new TdmWebHelp((TComponent*)0);
            MaRequete = new TZZMySqlQuery( (TComponent*)0);
            MaRequete->Database = dmWebHelp->dbWebHelp;

            MaRequete->Sql->Text = (AnsiString) "SELECT ref_adresse "+
                                                "FROM refer "+
                                                "WHERE (log_code= "+nLogiciel+
                                                " and ver_code="+nVersion+"  ) "+
                                                "and ref_nom="+QuotedStr(asRef);
            MaRequete->Open();
            MaRequete->First();
            while(!MaRequete->Eof) {
                if(!asRequete(0).IsEmpty())
                    bRetour = true;
                MaRequete->Next();
            }
            MaRequete->Sql->Clear();
            MaRequete->Close();
        }
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
        bRetour = false;}
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
    if(dmWebHelp) {delete dmWebHelp; dmWebHelp = NULL;}
    return bRetour;
}
//---------------------------------------------------------------------------
//               TOUT SELECTIONNER
//---------------------------------------------------------------------------
void __fastcall TfImport::bbSelectClick(TObject *Sender)
{
    try {
        for(int i=0; i<lvListe->Items->Count; i++)
            lvListe->Items->Item[i]->Selected = true;
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
//              TOUT DESELECTIONNER
//---------------------------------------------------------------------------
void __fastcall TfImport::bbUnSelectClick(TObject *Sender)
{
    try {
        for(int i=0; i<lvListe->Items->Count; i++)
            lvListe->Items->Item[i]->Selected = false;
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
//          Mettre en rouge les références qui n'ont pas d'adresse
//---------------------------------------------------------------------------
void __fastcall TfImport::lvListeCustomDrawSubItem(TCustomListView *Sender,
      TListItem *Item, int SubItem, TCustomDrawState State,
      bool &DefaultDraw)
{
    try {
        for(int i=0; i<Item->SubItems->Count; i++) {
            if( Item->SubItems->Strings[0] == NEANT )
                Sender->Canvas->Font->Color = clRed;
            if( Item->SubItems->Strings[1] == NEANT )
                Sender->Canvas->Font->Color = clGreen;
        }
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
//                          Quitter
//---------------------------------------------------------------------------
void __fastcall TfImport::bbQuitterClick(TObject *Sender)
{
    try {
        Close();
    }catch( Exception &e ) {
    EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
//
//---------------------------------------------------------------------------
void __fastcall TfImport::lvListeColumnClick(TObject *Sender,
      TListColumn *Column)
{
    try {
        nColumnToSort = Column->Index;
        if (nColumnToSort != nLastColumn) bAscToDesc = true;
        else                              bAscToDesc = !bAscToDesc;
        ((TCustomListView *)Sender)->AlphaSort();
        nLastColumn = nColumnToSort;

        // On efface les flèches
        for( int i=0;i<(( TListView*)Sender)->Columns->Count;i++)
            (( TListView*)Sender)->Columns->Items[i]->ImageIndex = -1;
        // On affiche une flèche indiquant le tri en cours
        Column->ImageIndex = (bAscToDesc)?enumArrow_ASC:enumArrow_DESC;

    } catch (Exception &e) {
        EIC_DISPLAY_EXCEPTION( e);}
}
//---------------------------------------------------------------------------
//
//---------------------------------------------------------------------------
void __fastcall TfImport::lvListeCompare(TObject *Sender, TListItem *Item1,
      TListItem *Item2, int Data, int &Compare)
{
    try {
        if (nColumnToSort == 0) {
            Compare = CompareText(Item1->Caption, Item2->Caption );
            if (!bAscToDesc) Compare = -Compare;
        } else {
            int ix = nColumnToSort - 1;
            Compare = CompareText( Item1->SubItems->Strings[ix], Item2->SubItems->Strings[ix] );
            if (!bAscToDesc) Compare = -Compare;
        }

    } catch (Exception &e) {
        EIC_DISPLAY_EXCEPTION( e);}
}
//---------------------------------------------------------------------------
//              Choix de la requete en fonction du filtre
//---------------------------------------------------------------------------
AnsiString __fastcall TfImport::ChoisirRequete()
{
    AnsiString asRequete = "";
    try {
        int nIdxLog = cbLogiciel->ItemIndex;
        int nIdxVer = cbVersion->ItemIndex;


        if( (nIdxLog != AUCUNE_SELECTION) && (nIdxVer != AUCUNE_SELECTION) ) {

            int nLogiciel = (int)cbLogiciel->Items->Objects[nIdxLog];
            int nVersion  = (int)cbVersion->Items->Objects[nIdxVer];

            if( (cbLien->Checked == true) && (cbRef->Checked == true) && (cbAucunEcart->Checked == true) )
                asRequete.sprintf(ALL_CHECKED,nLogiciel,nVersion,nLogiciel,nVersion);

            else if( (cbRef->Checked == true) && (cbAucunEcart->Checked == false) && (cbLien->Checked == false) )
                asRequete.sprintf(CBREF_CHECKED,nLogiciel,nVersion);

            else if( (cbLien->Checked == true) && (cbRef->Checked == false) && (cbAucunEcart->Checked == false) )
                asRequete.sprintf(CBLIEN_CHECHED,nLogiciel,nVersion);

            else if( (cbAucunEcart->Checked == true) && (cbRef->Checked == false) && (cbLien->Checked == false) )
                 asRequete.sprintf(CBAUCUNECART_CHECHED,nLogiciel,nVersion);

            else if( (cbRef->Checked == true) && (cbLien->Checked == true) && (cbAucunEcart->Checked == false) )
                 asRequete.sprintf(CBLIEN_CBREF_CHECHED,nLogiciel,nVersion,nLogiciel,nVersion);

            else if( (cbRef->Checked == true) && (cbAucunEcart->Checked == true) && (cbLien->Checked == false) )
                 asRequete.sprintf(CBREF_CBAUCUNECART_CHECHED,nLogiciel,nVersion,nLogiciel,nVersion);

            else if((cbLien->Checked == true) && (cbAucunEcart->Checked == true) && (cbRef->Checked == false) )
                 asRequete.sprintf(CBLIEN_CBAUCUNECART_CHECHED,nLogiciel,nVersion,nLogiciel,nVersion);
        }
    } catch ( Exception &e ) {
        EIC_DISPLAY_EXCEPTION( e);
        asRequete = "";}
    return asRequete;
}
//---------------------------------------------------------------------------
//                          Filtres
//---------------------------------------------------------------------------
void __fastcall TfImport::cbLienClick(TObject *Sender)
{
    try {
        Afficher();
    } catch ( Exception &e ) {
    EIC_DISPLAY_EXCEPTION( e);}
}
//---------------------------------------------------------------------------
void __fastcall TfImport::cbRefClick(TObject *Sender)
{
    try {
        Afficher();
    } catch ( Exception &e ) {
    EIC_DISPLAY_EXCEPTION( e);}
}
//---------------------------------------------------------------------------
void __fastcall TfImport::cbAucunEcartClick(TObject *Sender)
{
    try {
        Afficher();
    } catch ( Exception &e ) {
    EIC_DISPLAY_EXCEPTION( e);}
}
//---------------------------------------------------------------------------
void __fastcall TfImport::sbRazFiltreClick(TObject *Sender)
{
    try {
        cbRef->Checked = true;
        cbAucunEcart->Checked = true;
        cbLien->Checked = true;
    } catch ( Exception &e ) {
    EIC_DISPLAY_EXCEPTION( e);}
}
//---------------------------------------------------------------------------

